package database;

import java.awt.*;
import javax.swing.*;
import javax.swing.tree.*;
import java.util.*;
import java.awt.event.*;
import java.lang.reflect.Method;


public class DrawTree extends JFrame implements MouseListener {

  private JTree tree;
  public DrawTree(ArrayList<topics.MainTopic> lessn) {
    super("Lesson in Tree Form");
    WindowUtilities.setNativeLookAndFeel();

    Container content = getContentPane();

    DefaultMutableTreeNode root = new DefaultMutableTreeNode("LESSON");
    for(int i = 0; i < lessn.size(); i++)
    {
         topics.MainTopic mt = lessn.get(i);
         DefaultMutableTreeNode mainNode = new DefaultMutableTreeNode(mt.getTopic());
         root.add(mainNode);
         for(int j = 0; j < mt.subSize(); j ++)
         {
             topics.SubTopic st = mt.getSubTopic(j);
             DefaultMutableTreeNode subNode = new DefaultMutableTreeNode(st.getTopic());
             mainNode.add(subNode);
             for(int k = 0; k < st.microSize(); k++)
             {
                 topics.MicroTopic micro = st.getMicroTopic(k);
                 DefaultMutableTreeNode microNode = new DefaultMutableTreeNode(micro.getTopic());
                 subNode.add(microNode);
             }
         }
    }

     tree = new JTree(root);
     MouseListener ml = new MouseAdapter()
     {
            @Override
        public void mousePressed(MouseEvent e)
        {
            int selRow = tree.getRowForLocation(e.getX(), e.getY());
            TreePath selPath = tree.getPathForLocation(e.getX(), e.getY());
            if(selRow != -1)
            {
                if(e.getClickCount() == 2)
                {
                    String firpart = "http://www.google.com/search?hl=en&q=";
                    String endpart = "&btnI=I%27m+Feeling+Lucky&aq=f&oq=";
                    StringTokenizer search = new StringTokenizer(selPath.getPathComponent(selPath.getPathCount()-1).toString());
                    String constructed = "";
                    String osName = System.getProperty("os.name");
                    while(search.hasMoreTokens())
                    {
                        constructed += search.nextToken() + "+";
                    }
                    try
                    {
                    if (osName.startsWith("Mac OS"))
                    {
                        Class fileMgr = Class.forName("com.apple.eio.FileManager");
                        Method openURL = fileMgr.getDeclaredMethod("openURL",
                        new Class[] {String.class});
                        openURL.invoke(null, new Object[] {firpart+constructed+endpart});
                    }
                    else if (osName.startsWith("Windows"))
                        Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " +
                                    firpart+constructed+endpart);
                    else
                    {   //assume Unix or Linux
                        String[] browsers = {
                        "firefox", "opera", "konqueror", "epiphany", "mozilla", "netscape" };
                        String browser = null;
                        for (int count = 0; count < browsers.length && browser == null; count++)
                        if (Runtime.getRuntime().exec(
                            new String[] {"which", browsers[count]}).waitFor() == 0)
                            browser = browsers[count];
                        if (browser == null)
                        throw new Exception("Could not find web browser");
                        else
                        Runtime.getRuntime().exec(new String[] {browser, firpart+constructed+endpart});
                    }
                }
                    catch (Exception ex)
                    {
                        JOptionPane.showMessageDialog(null, "Problem Opening Browser." +
                                ":\n" + ex.getLocalizedMessage());
                    }
      }
                }
            }
        };
        
    tree.addMouseListener(ml);

    content.add(new JScrollPane(tree), BorderLayout.CENTER);
    setBounds(375, 200, 275, 300);
    setVisible(true);
  }

    public void mouseClicked(MouseEvent e) {}

    public void mousePressed(MouseEvent e){}

    public void mouseReleased(MouseEvent e) {}

    public void mouseEntered(MouseEvent e) {}

    public void mouseExited(MouseEvent e) {}

}